﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace Sbook.Data
{
    public class AuthorsController:SqlDataProvider
    {
        #region[Authors_GetById]
        public List<AuthorsInfo> Authors_GetById(string Id)
        {
            List<Data.AuthorsInfo> list = new List<Data.AuthorsInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Authors_GetById", GetConnection()))
            {
                Data.AuthorsInfo obj = new Data.AuthorsInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@AId", Id));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.AuthorsIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[Authors_GetByTop]
        public List<AuthorsInfo> Authors_GetByTop(string Top, string Where, string Order)
        {
            List<Data.AuthorsInfo> list = new List<Data.AuthorsInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Authors_GetByTop", GetConnection()))
            {
                Data.AuthorsInfo obj = new Data.AuthorsInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Top", Top));
                dbCmd.Parameters.Add(new SqlParameter("@Where", Where));
                dbCmd.Parameters.Add(new SqlParameter("@Order", Order));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.AuthorsIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[Authors_GetByAll]
        public List<AuthorsInfo> Authors_GetByAll()
        {
            List<Data.AuthorsInfo> list = new List<Data.AuthorsInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Authors_GetByAll", GetConnection()))
            {
                Data.AuthorsInfo obj = new Data.AuthorsInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.AuthorsIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[Authors_Paging]
        public List<AuthorsInfo> Authors_Paging(string CurentPage, string PageSize)
        {
            List<Data.AuthorsInfo> list = new List<Data.AuthorsInfo>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Authors_Paging", GetConnection()))
            {
                Data.AuthorsInfo obj = new Data.AuthorsInfo();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@CurentPage", CurentPage));
                dbCmd.Parameters.Add(new SqlParameter("@PageSize", PageSize));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.AuthorsIDataReader(dr));
                    }
                    dr.Close();
                    //conn.Close();
                }
            }
            return list;
        }
        #endregion
        #region[Authors_Insert]
        public bool Authors_Insert(AuthorsInfo data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Authors_Insert", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@AName", data.AName));
                dbCmd.Parameters.Add(new SqlParameter("@APhone", data.APhone));
                dbCmd.Parameters.Add(new SqlParameter("@AAddress", data.AAddress));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Authors");
            return true;
        }
        #endregion
        #region[Authors_Update]
        public bool Authors_Update(AuthorsInfo data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Authors_Update", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@AId", data.AId));
                dbCmd.Parameters.Add(new SqlParameter("@AName", data.AName));
                dbCmd.Parameters.Add(new SqlParameter("@APhone", data.APhone));
                dbCmd.Parameters.Add(new SqlParameter("@AAddress", data.AAddress));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Authors");
            return true;
        }
        #endregion
        #region[Authors_Delete]
        public bool Authors_Delete(string Id)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Authors_Delete", GetConnection()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@AId", Id));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Authors");
            return true;
        }
        #endregion
    }
}